﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace MemoryDuosRankings
{
    public class DAL
    {
        private SQLiteDatabase DB { get; set; }
        private RankingDAO rDAO;
        private static readonly Lazy<DAL> instance = new Lazy<DAL>(() => new DAL());

        private DAL()
        {
            DB = new SQLiteDatabase(AppDomain.CurrentDomain.BaseDirectory+"Resources/bd.sqlite");
            rDAO = new RankingDAO(DB);
            Inicializar();
        }

        public static DAL Instance
        {
            get
            {
                return instance.Value;
            }
        }

        private void Inicializar()
        {
            try
            {
                DAL.Instance.HayPartidaSinFinalizar();
            }
            catch (Exception exc) {
                using (StreamReader lector = new StreamReader(AppDomain.CurrentDomain.BaseDirectory+"Resources/PartidaEstablecer.sql"))
                {
                    string sql = lector.ReadToEnd();
                    DB.ExecuteNonQuery(sql);
                }
            }
        }
        public int AñadirRanking(Ranking r) { return rDAO.AñadirRanking(r); }
        public List<Ranking> GetRankings() { return rDAO.GetRankings(); }
        public List<Ranking> GetRankings(int nivel) { return rDAO.GetRankings(nivel); }
        public void FinalizarRanking(Ranking r,TimeSpan p) { rDAO.FinalizarRanking(r,p); }
        public void LimpiarRankings() { DB.ClearTable("PartidaEstablecer"); }
        public Ranking ObtenerPartidaSinFinalizar() { return rDAO.ObtenerPartidaSinFinalizar(); }
        public bool HayPartidaSinFinalizar() { return rDAO.HayPartidaSinFinalizar(); }
        public void EliminarRanking(Ranking r) { rDAO.EliminarRanking(r); }
        public void PausarRanking(Ranking r, TimeSpan p,String galería, List<Tuple<int,int,int>> lista) { rDAO.PararRanking(r, p, galería,lista); }
    }
}
